package com.googlecode.scenemaker3d.view.timeline
{
	import com.googlecode.scenemaker3d.controller.IObjectController;
	import com.googlecode.scenemaker3d.controller.ITimelineController;
	
	import com.googlecode.scenemaker3d.events.HideMarkerMoveEvent;
	import com.googlecode.scenemaker3d.events.KeyFrameMoveEvent;
	import com.googlecode.scenemaker3d.events.Position3DEvent;
	import com.googlecode.scenemaker3d.events.SelectedObject3DEvent;
	
	import flash.display.Sprite;
	import flash.events.ContextMenuEvent;
	import flash.events.Event;
	import flash.ui.ContextMenu;
	import flash.ui.ContextMenuItem;
	
	import com.googlecode.scenemaker3d.model.VO_Object3D;
	import com.googlecode.scenemaker3d.model.World3DModel;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.HDividedBox;
	import mx.containers.VBox;
	import mx.core.ScrollPolicy;
	import mx.core.UIComponent;
	import mx.events.CollectionEvent;
	
	import org.papervision3d.core.math.Number3D;
	
	import com.googlecode.scenemaker3d.services.Control;
	
	import com.googlecode.scenemaker3d.view.DefaultTextFormat;
	

	public class Timeline extends VBox
	{	
		private var wModel:World3DModel;
		private var cTimeline:ITimelineController;
		private var cObjects:IObjectController;
		private var tFormat:DefaultTextFormat = new DefaultTextFormat();

		private var mainHBox:HDividedBox = new HDividedBox();
		private var controlsHBox:HDividedBox = new HDividedBox();
		private var pane:InfoPane = new InfoPane();
		
		private var gridBox:Canvas = new Canvas();
		private var timeline:UIComponent = new UIComponent();
		private var thumb:Thumb;
		private var transport:TransportControls = new TransportControls();
		private var tWidth:uint = 600;
		private var tPercentWidth:uint = 0;
		
		private var grid:Grid;
		private var gridNumSpacing:uint = 10;	
		public static const SQUARE_WIDTH:uint = 10;
		
		private var keyFrameCopy:KeyFrame = null;
		private var autoKeyFraming:Boolean = true;
		
		private var vertOffset:int = 30;
		
		private var sm:SplitMarker;
				
		public function Timeline()
		{
			super();
			
			wModel = Control.instance.world3DModel;
			cTimeline = Control.instance.timelineController;
			cObjects = Control.instance.objectController;
			
			this.horizontalScrollPolicy = ScrollPolicy.OFF;
			this.verticalScrollPolicy = ScrollPolicy.OFF;
			this.setStyle("backgroundColor", 0xCCCCCC);
			this.percentHeight = 100;
			//this.width = tWidth;
			
			wModel.objects.addEventListener(CollectionEvent.COLLECTION_CHANGE, onObjectEvent);
			wModel.addEventListener(SelectedObject3DEvent.SELECTED_OBJECT_3D, onSelectObject);
			wModel.addEventListener(Position3DEvent.POSITION_3D, onPosition3DChange);
			
			thumb = new Thumb(SQUARE_WIDTH, SQUARE_WIDTH);
			
			thumb.addEventListener("thumbUp", onThumbUp);
			thumb.addEventListener("thumbMove", onThumbMove);
			
			tFormat.bold = true;
			tFormat.indent = 10;
			//Add a container for all the timeline parts
			makeMainHBox();	
			makeInfoBox();
			makeGridBox();
			makeTimelineContainer(gridNumSpacing * SQUARE_WIDTH * 10);
			makeTimeline(gridNumSpacing * SQUARE_WIDTH * 10);
			makeControlsHBox();
			
		}
		private function onThumbUp(e:Event):void
		{
			setCurrentTimeInterval(e.currentTarget.x);
			scrubObjects3D();		
		}
		private function onThumbMove(e:Event):void
		{
			setCurrentTimeInterval(e.currentTarget.x);
			scrubObjects3D();
			
			//If thumb has gone out of bounds, adjust scroller
			if((thumb.x - gridBox.horizontalScrollPosition + SQUARE_WIDTH) > gridBox.width)
			{
				gridBox.horizontalScrollPosition += SQUARE_WIDTH;
			}
			if(thumb.x >= timeline.width)
			{
				thumb.x = (Math.floor(timeline.width /SQUARE_WIDTH) * SQUARE_WIDTH) - SQUARE_WIDTH;
				cTimeline.selectedTimeInterval = thumb.x;
				
				return;	
			}
			if(thumb.x < gridBox.horizontalScrollPosition + SQUARE_WIDTH)
			{
			gridBox.horizontalScrollPosition -= SQUARE_WIDTH;
			}
				
			if(thumb.x == 0)
			{
				gridBox.horizontalScrollPosition = 0;	
			}		
		}
		
		private function onSelectObject(e:SelectedObject3DEvent):void
		{
			pane.objTxt = e.VO.label;

			loadKeyFrames(e.VO.keyFrames);
			loadHideMarkers(e.VO.hideMarkers);
			
			//Check object has moved
			if(e.VO.lastPos)
			{
				if(e.VO.lastPos.x == e.VO.data.x && e.VO.lastPos.y == e.VO.data.y && e.VO.lastPos.z == e.VO.data.z)
				{
					return;
					
				}
			}
			var newPos:Number3D = new Number3D(e.VO.data.x, e.VO.data.y, e.VO.data.z);
			e.VO.lastPos = newPos;
		}
		private function onPosition3DChange(e:Position3DEvent):void
		{
			if(e.VO == null)
			{
				return;
			}
			var newPos:Number3D = new Number3D(e.VO.data.x, e.VO.data.y, e.VO.data.z);
			if(e.VO.lastPos)
			{
				if(newPos.x == e.VO.lastPos.x && newPos.y == e.VO.lastPos.y && newPos.z == e.VO.lastPos.z)
				{
					return;
				}
			}
			e.VO.lastPos = newPos;

			if(!autoKeyFraming)
			{
				return;
			}
			
			cTimeline.insertKeyFrame();	
			
			
			loadKeyFrames(e.VO.keyFrames);
			
			//Finally, rewrite the tweens.
			cObjects.setSelectedObject(e.VO.id);
			cTimeline.calculateTweens(e.VO.keyFrames);
			scrubObjects3D();	
		}
		
		private function onGlobalMarkerDelete(e:ContextMenuEvent):void
		{
			var id:String = (e.contextMenuOwner as GlobalMarker).id;
			cTimeline.removeGlobalMarker(id);
			
			loadGlobalMarkers(wModel.globalMarkers);
		}		
		private function onGlobalMarkerDeleteAll(e:ContextMenuEvent):void
		{
			cTimeline.removeAllGlobalMarkers();
			removeAllGlobalMarkers();
		}
		
		private function onKeyFrameMove(e:KeyFrameMoveEvent):void
		{
			//Stop the keyframe from moving if at start position.
			if(e.keyFrame.time == 0)
			{
				e.keyFrame.enableDragging = false;
			}
				
			var VO:VO_Object3D = wModel.selectedObject;
			
			if(!VO)
			{
				return;
			}
			var keyFrames:ArrayCollection = VO.keyFrames;
			var newTime:Number = e.keyFrame.time;
			
			for(var i:uint=0; i<keyFrames.length; i++)
			 {
				if(keyFrames[i].time == newTime && keyFrames[i] != e.keyFrame)
				{
					cTimeline.removeKeyFrame(keyFrames[i]);
					
					loadKeyFrames(keyFrames);
					
					break;	
				}		
			}
			//Finally, rewrite the tweens.
			cTimeline.calculateTweens(keyFrames);  
			scrubObjects3D(); 
		}
		private function onKeyFrameDelete(e:ContextMenuEvent):void
		{
			if(e.target.label != "Delete keyframe")
			{
				return;
			}
			var keyFrame:KeyFrame = e.contextMenuOwner as KeyFrame;
			
			if(keyFrame.time == 0)
			
			{
				return;
			}
			
			var currentAutoKeyframing:Boolean = autoKeyFraming;
			autoKeyFraming = false;
			
			var VO:VO_Object3D = keyFrame.object3D;
			var keyFrames:ArrayCollection = VO.keyFrames;
			
			cTimeline.removeKeyFrame(keyFrame);
			cTimeline.calculateTweens(VO.keyFrames);
			
			if(wModel.currentTimeInt <= VO.tweens.length -1)
			{
				//trace(VO.tweens.getItemAt(wModel.currentTimeInt));
				var newPos:Number3D = (VO.tweens.getItemAt(wModel.currentTimeInt) as Number3D);
			}else{
				//trace("£"+VO.tweens.getItemAt(VO.tweens.length -1));
				newPos = (VO.tweens.getItemAt(VO.tweens.length -1) as Number3D);
			}
			
			cObjects.setObjectPos(VO.id, newPos);
			
			loadKeyFrames(VO.keyFrames);
			
			autoKeyFraming = currentAutoKeyframing;
			
		}
		private function onKeyFrameDeleteAll(e:ContextMenuEvent):void
		{
			if(e.target.label == "Delete all keyframes")
			{
				var currentAutoKeyframing:Boolean = autoKeyFraming;
				autoKeyFraming = false;
				
				var keyFrame:KeyFrame = e.contextMenuOwner as KeyFrame;
				var VO:VO_Object3D = keyFrame.object3D;
				var keyFrames:ArrayCollection = VO.keyFrames;
				
				cTimeline.removeAllKeyFrames(VO);
				loadKeyFrames(keyFrames);
				
				//Finally, rewrite the tweens.
				cTimeline.calculateTweens(keyFrames);
				
				cObjects.setObjectPos(VO.id, keyFrames[0].objPos);
				
				autoKeyFraming = currentAutoKeyframing;
			}
		}
		private function onKeyFrameCopy(e:ContextMenuEvent):void
		{
			if(e.target.label == "Copy keyframe"){
				if(e.contextMenuOwner as KeyFrame)
				{
					keyFrameCopy = e.contextMenuOwner as KeyFrame;
				}
			}
		}
		private function onKeyFramePaste(e:ContextMenuEvent):void
		{	
			if(e.target.label != "Paste keyframe")
			{
				return;
			}

			if(keyFrameCopy as KeyFrame)
			{
				var VO:VO_Object3D = wModel.selectedObject;
				var time:uint = Math.floor(e.mouseTarget.mouseX / SQUARE_WIDTH) * SQUARE_WIDTH;
				cTimeline.insertKeyFrame(time, keyFrameCopy.objPos);
				loadKeyFrames(VO.keyFrames);
				
				//Move thumb and update time
				thumb.x = time;
				cTimeline.selectedTimeInterval = time;
				
				//Finally, rewrite the tweens.
				cTimeline.calculateTweens(VO.keyFrames);
				scrubObjects3D();
			}
		}
		
		private function onHideMarkerMove(e:HideMarkerMoveEvent):void
		{
			scrubObjects3D();
		}
		private function onHideMarkerDelete(e:ContextMenuEvent):void
		{
			if(e.target.label != "Delete hide marker")
			{
				return;
			}
			var hideMarker:SplitMarker = e.contextMenuOwner as SplitMarker;
			
			var VO:VO_Object3D = hideMarker.object3D;
			var hideMarkers:ArrayCollection = VO.hideMarkers;
			
			cTimeline.removeHideMarker(hideMarker);
			
			loadHideMarkers(hideMarkers);
			
		}
		private function onHideMarkerDeleteAll(e:ContextMenuEvent):void
		{	
			var hideMarker:SplitMarker = e.contextMenuOwner as SplitMarker;
			var VO:VO_Object3D = hideMarker.object3D;
			var hideMarkers:ArrayCollection = VO.hideMarkers;
			
			cTimeline.removeAllHideMarkers(VO);
			loadHideMarkers(hideMarkers);
		}
			
		private function onAddGlobalMarker(e:Event):void
		{
			
			cTimeline.insertGlobalMarker();
			var globalMarkers:ArrayCollection = wModel.globalMarkers;
			
			loadGlobalMarkers(globalMarkers);
			
		}			
		private function onAddPositionKeyFrame(e:Event):void
		{
			var VO:VO_Object3D = wModel.selectedObject;
			
			if(VO as VO_Object3D == null)
			{
				return;
			}
			
			cTimeline.insertKeyFrame();	

			loadKeyFrames(VO.keyFrames);
			
			//Finally, rewrite the tweens.
			cTimeline.calculateTweens(VO.keyFrames);
			//scrubObjects3D(); 
		}
		private function onAddHideMarker(e:Event):void
		{
			var VO:VO_Object3D = wModel.selectedObject;
			//Cannot insert a marker into a null object
			if(VO == null)
			{
				return;
			}
			
			//Check that there is not already a marker covering that position
			var time:uint = cTimeline.selectedTimeInterval;
			
			for(var i:uint=0; i<VO.hideMarkers.length; i++)
			{
				var hideMarker:SplitMarker = VO.hideMarkers[i];
				var start:uint = hideMarker.marker.start;
				var end:uint = hideMarker.marker.end;
				
				if(start <= time && time <= end)
				{
					return;
				}
				
			}

			var yOffset:uint = vertOffset + (SQUARE_WIDTH * 2);
			var hide:SplitMarker = new SplitMarker(SQUARE_WIDTH, SQUARE_WIDTH, time, yOffset);
				
			cTimeline.insertHideMarker(yOffset);
			loadHideMarkers(VO.hideMarkers);
			//timeline.addChild(hide);
		}
		private function onExtendTimeline(e:Event):void
		{
			var tWidth:uint = timeline.width;
			timeline.width = tWidth + (SQUARE_WIDTH * gridNumSpacing * 10);
			makeTimeline(tWidth + (SQUARE_WIDTH * gridNumSpacing * 10));
			trace(timeline.width);
		}

		/* private function onMainHBoxResize(e:ResizeEvent):void
		{
			trace(timeline.parent.width+"£");
			mainHBox.width = 1000;
		} */
		private function makeMainHBox():void
		{
			//mainHBox.height = 120;
			//mainHBox.addEventListener(ResizeEvent.RESIZE, onMainHBoxResize);
			mainHBox.setStyle("backgroundColor", 0x999999);
			mainHBox.setStyle("horizontalGap", 3);
			
			mainHBox.horizontalScrollPolicy = ScrollPolicy.OFF;
			mainHBox.verticalScrollPolicy = ScrollPolicy.OFF;
			
			this.addChild(mainHBox); 
		}
		private function makeControlsHBox():void
		{
			transport.addEventListener("stopMarker", onAddGlobalMarker);
			transport.addEventListener("positionKeyFrame", onAddPositionKeyFrame);
			transport.addEventListener("hideMarker", onAddHideMarker);
			transport.addEventListener("extendTimeline", onExtendTimeline);
			
			controlsHBox.addChild(transport);
			controlsHBox.width = tWidth;
			
			this.addChild(controlsHBox);
		}
		private function makeInfoBox():void
		{
			pane.vertOffset = vertOffset;
			pane.spacing = SQUARE_WIDTH * 2;
			pane.paneHeight = 130;

			mainHBox.addChild(pane);
		}
		private function makeGridBox():void
		{
			gridBox.width = 200;
			//gridBox.height = 150;
			gridBox.verticalScrollPolicy = ScrollPolicy.OFF;
			//gridBox.addEventListener(MouseEvent.ROLL_OUT, onGridBoxRollOut);

			mainHBox.addChild(gridBox);
		}
		private function makeTimelineContainer(w:uint):void
		{
			timeline.width = w;
			timeline.height = 130;
			
			gridBox.addChild(timeline);
		}
		private function makeTimeline(w:uint):void
		{
			//Remove strip
			for(var i:uint=0; i<timeline.numChildren; i++)
			{
				if(timeline.getChildAt(i).name == "keyFrameStrip")
				{
					timeline.removeChildAt(i);
					break;
				}		
			}
			if(grid as Grid == null)
			{
				grid = new Grid(w, 7, SQUARE_WIDTH, gridNumSpacing);
				timeline.addChild(thumb);
			}
			
			grid.makeGrid(w, 7, SQUARE_WIDTH, gridNumSpacing);
			
			grid.y = vertOffset;
			grid.mouseEnabled = false;
			
			//Make a strip that can be right-clicked
			var paste:ContextMenuItem = new ContextMenuItem("Paste keyframe");
			paste.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onKeyFramePaste);
			
			var customContextMenu:ContextMenu = new ContextMenu();
			customContextMenu.hideBuiltInItems();
			customContextMenu.customItems.push(paste);

			var keyFrameStrip:Sprite = new Sprite();
			keyFrameStrip.name = "keyFrameStrip";
			keyFrameStrip.graphics.beginFill(0x000000, 0);
			keyFrameStrip.graphics.drawRect(0, 0, w * SQUARE_WIDTH, SQUARE_WIDTH);
			keyFrameStrip.contextMenu = customContextMenu;
			
			keyFrameStrip.y = vertOffset + (SQUARE_WIDTH * 2);
					
			
			thumb.y = 10;
			
			timeline.addChild(grid);
			timeline.addChild(keyFrameStrip);
			timeline.setChildIndex(grid, 0);
			timeline.setChildIndex(keyFrameStrip, 1);
			timeline.setChildIndex(thumb, timeline.numChildren -1);
		}
		
		private function setCurrentTimeInterval(t:uint):void
		{
			cTimeline.selectedTimeInterval = t;
		}
		
		private function onObjectEvent(e:CollectionEvent):void
		{	
			var VO:VO_Object3D = e.items[0] as VO_Object3D;
			
			switch(e.kind)
			{
				case "add":	
					cObjects.setSelectedObject(VO.id);
											
					loadKeyFrames(VO.keyFrames);
					loadHideMarkers(VO.hideMarkers);
					
					cTimeline.calculateTweens(VO.keyFrames);
					scrubObjects3D();
					
					pane.objTxt = VO.label;
					dispatchEvent(new Event("objLabelChange"));

					break;
				case "remove":
					for(var i:uint=0; i<VO.keyFrames.length; i++)
					{
						removeKeyFrame(VO.keyFrames[i].id);
						pane.objTxt = "";
					} 
					for(i=0; i<VO.hideMarkers.length; i++)
					{
						removeHideMarker(VO.hideMarkers[i].id);
					}
					cObjects.setSelectedObject(null);	
					break;
			}
		}
		
		private function loadKeyFrames(keyFrames:ArrayCollection):void
		{	
			removeAllKeyFrames();
			addKeyFrameContextMenus(keyFrames);
			

			for(var i:uint=0; i<keyFrames.length; i++)
			{
				(keyFrames[i] as KeyFrame).y = vertOffset;
				(keyFrames[i] as KeyFrame).addEventListener(KeyFrameMoveEvent.KEY_FRAME_MOVE, onKeyFrameMove);
				timeline.addChild(keyFrames[i]);
			}
		}
		private function addKeyFrameContextMenus(keyFrames:ArrayCollection):void
		{
			//Add right-click listener
			var del:ContextMenuItem = new ContextMenuItem("Delete keyframe");
			del.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onKeyFrameDelete);
			
			var delAll:ContextMenuItem = new ContextMenuItem("Delete all keyframes");
			delAll.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onKeyFrameDeleteAll);
			
			var copy:ContextMenuItem = new ContextMenuItem("Copy keyframe");
			copy.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onKeyFrameCopy);
			
			var paste:ContextMenuItem = new ContextMenuItem("Paste keyframe");
			copy.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onKeyFramePaste);
			
			var customContextMenu:ContextMenu = new ContextMenu();
			customContextMenu.hideBuiltInItems();
			customContextMenu.customItems.push(del);
			customContextMenu.customItems.push(delAll);
			customContextMenu.customItems.push(copy);
			customContextMenu.customItems.push(paste);
			
			for(var i:uint=0; i<keyFrames.length; i++)
			{
				(keyFrames[i] as KeyFrame).contextMenu = customContextMenu;
			}
			
		}
		private function removeKeyFrame(id:String):void
		{		
			for(var i:uint=0; i<timeline.numChildren; i++)
			{
				if(timeline.getChildAt(i) as KeyFrame)
				{			
					if((timeline.getChildAt(i) as KeyFrame).id == id)
					{	
						timeline.getChildAt(i).removeEventListener(KeyFrameMoveEvent.KEY_FRAME_MOVE, onKeyFrameMove);
						timeline.removeChild(timeline.getChildAt(i));
						break;
					}
				}
			}
		}
		private function removeAllKeyFrames():void
		{
			for(var i:uint=0; i<timeline.numChildren; i++)
			{
				if(timeline.getChildAt(i) as KeyFrame)
				{
					timeline.getChildAt(i).removeEventListener(KeyFrameMoveEvent.KEY_FRAME_MOVE,onKeyFrameMove);
					timeline.removeChild(timeline.getChildAt(i));
					i--;
				}
			}	
		}
		
		private function loadHideMarkers(hideMarkers:ArrayCollection):void
		{
			removeAllHideMarkers();
			addHideMarkerContextMenus(hideMarkers);
			
			for(var i:uint=0; i<hideMarkers.length; i++)
			{
				(hideMarkers[i] as SplitMarker).addEventListener(HideMarkerMoveEvent.HIDE_MARKER_MOVE, onHideMarkerMove);
				timeline.addChild(hideMarkers[i]);
				
			}
			scrubObjects3D();
		}
		private function removeHideMarker(id:String):void
		{		
			for(var i:uint=0; i<timeline.numChildren; i++)
			{
				if(timeline.getChildAt(i) as SplitMarker)
				{			
					if((timeline.getChildAt(i) as SplitMarker).id == id)
					{	
						timeline.getChildAt(i).removeEventListener(HideMarkerMoveEvent.HIDE_MARKER_MOVE, onHideMarkerMove);
						timeline.removeChild(timeline.getChildAt(i));
						break;
					}
				}
			}
			//scrubObjects3D();
		}
		private function removeAllHideMarkers():void
		{
			for(var i:uint=0; i<timeline.numChildren; i++)
			{
				if(timeline.getChildAt(i) as SplitMarker)
				{
					timeline.getChildAt(i).removeEventListener(HideMarkerMoveEvent.HIDE_MARKER_MOVE, onHideMarkerMove);
					timeline.removeChild(timeline.getChildAt(i));
					i--;
				}
			}
		}

		private function addHideMarkerContextMenus(hideMarkers:ArrayCollection):void
		{
			//Add right-click listener
			var del:ContextMenuItem = new ContextMenuItem("Delete hide marker");
			del.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onHideMarkerDelete);
			
			var delAll:ContextMenuItem = new ContextMenuItem("Delete all hide markers");
			delAll.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onHideMarkerDeleteAll);
			
			var customContextMenu:ContextMenu = new ContextMenu();
			customContextMenu.hideBuiltInItems();
			customContextMenu.customItems.push(del);
			customContextMenu.customItems.push(delAll);
			
			for(var i:uint=0; i<hideMarkers.length; i++)
			{
				(hideMarkers[i] as SplitMarker).contextMenu = customContextMenu;
			}
		}
		
		private function loadGlobalMarkers(globalMarkers:ArrayCollection):void
		{
			removeAllGlobalMarkers();
			addGlobalMarkerContextMenus(globalMarkers);
			
			for(var i:uint=0; i<globalMarkers.length; i++)
			{
				globalMarkers[i].x = globalMarkers[i].time;
				timeline.addChild(globalMarkers[i]);	
			}
		}
		private function removeAllGlobalMarkers():void
		{
			for(var i:uint=0; i<timeline.numChildren; i++)
			{
				if(timeline.getChildAt(i) as GlobalMarker)
				{
					timeline.removeChild(timeline.getChildAt(i));
					i--;
				}
			}
		}
		private function addGlobalMarkerContextMenus(globalMarkers:ArrayCollection):void
		{
			//Add right-click listener
			var del:ContextMenuItem = new ContextMenuItem("Delete global marker");
			del.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onGlobalMarkerDelete);
			
			var delAll:ContextMenuItem = new ContextMenuItem("Delete all global markers");
			delAll.addEventListener(ContextMenuEvent.MENU_ITEM_SELECT, onGlobalMarkerDeleteAll);
			
			var customContextMenu:ContextMenu = new ContextMenu();
			customContextMenu.hideBuiltInItems();
			customContextMenu.customItems.push(del);
			customContextMenu.customItems.push(delAll);
			
			for(var i:uint=0; i<globalMarkers.length; i++)
			{
				(globalMarkers[i] as GlobalMarker).contextMenu = customContextMenu;
			}
		}
		private function scrubObjects3D():void
		{	
			var currentAutoKeyFraming:Boolean = autoKeyFraming;
			autoKeyFraming = false;
			//Firstly, check to see whether the object should be part of the scene or not
			//All of the positions and hide markers of the objects are recorded in the tweens AC.
			var objects3D:ArrayCollection = wModel.objects;
			//var VO:VO_Object3D;
			var time:uint = cTimeline.selectedTimeInterval /10;
			
			for(var i:uint=0; i<objects3D.length; i++)
			{
				var VO:VO_Object3D = objects3D[i];
				var hideMarkers:ArrayCollection = VO.hideMarkers;
				
				trace(hideOrShow(hideMarkers));
				if(hideOrShow(hideMarkers))
				{
					VO.data.visible = false;
				}else
				{
					VO.data.visible = true;
				}
				
				//Check we are in range
				if(VO.tweens.length > 0 &&  VO.tweens.length > time )
				{
					cObjects.setObjectPos(VO.id, VO.tweens[time]);
					/* VO.data.x = VO.tweens[time].x;
					VO.data.y = VO.tweens[time].y;
					VO.data.z = VO.tweens[time].z; */
				}else{
					//
				}
			}
			autoKeyFraming = currentAutoKeyFraming;
		}
		private function hideOrShow(hideMarkers:ArrayCollection):Boolean
		{
			var time:uint = cTimeline.selectedTimeInterval;

			for(var i:uint=0; i<hideMarkers.length; i++){
				var hideMarker:SplitMarker = hideMarkers[i];
				
				if(time >= hideMarker.marker.start && time <= hideMarker.marker.end)
				{
					return true;
				} 	
			}
			return false;
		}
		
		public function set timelineWidth(w:uint):void
		{
			tWidth = w;
		}
		public function set timelinePercentWidth(w:uint):void
		{
			tPercentWidth = w;
		}
		override protected function commitProperties():void
		{
			if(tPercentWidth > 0)
			{
				mainHBox.percentWidth = tPercentWidth;
				this.percentWidth = tPercentWidth;
			}else{
				mainHBox.percentWidth = tWidth;
				this.width = tWidth;
			}
			
			//mainHBox.width = tWidth;
			
		}
	}
}